<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="header-left">
        <div class="avatar_box">
          <img src="../assets/logo.jpg" alt="logo">
        </div>
        <span>Vue-Shop</span>
      </div>
      <div class="header-right">
        <el-button id="logout"
                   @click="logout">
          登&nbsp;&nbsp;出
        </el-button>
      </div>
    </el-header>
    <!-- 页面主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button"
             @click="toggleCollapse">
          <i :class="isCollapse ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"></i>
        </div>
        <!-- 侧边栏菜单 -->
        <el-menu background-color="#a7d1df"
                 text-color="#fff"
                 active-text-color="#ffdd94"
                 unique-opened
                 :collapse-transition="false"
                 :collapse="isCollapse"
                 :default-active="activePath"
                 router>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id.toString()"
                      v-for="item in menuList"
                      :key="item.id">
            <!-- 标题 -->
            <template #title>
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span class="index-1">{{ item.authName }}</span>
            </template>
            <!-- 选项 -->
            <el-menu-item :index="'/'+subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavState('/'+subItem.path)">
              <!-- 标题 -->
              <template #title>
                <!-- 图标 -->
                <i class="el-icon-arrow-right"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      menuList: [],
      isCollapse: false,
      iconsObj: {
        125: 'el-icon-user',
        103: 'el-icon-key',
        101: 'el-icon-shopping-bag-1',
        102: 'el-icon-notebook-1',
        145: 'el-icon-pie-chart'
      },
      activePath: ''
    }
  },
  methods: {
    logout () {
      this.$confirm('确定退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        // 清空 sessionStorage 完成登出
        window.sessionStorage.clear()
        this.$message.success('成功登出')
        this.$router.push('/login')
      })
    },
    async getMenuList () {
      // 请求获取菜单
      const { data: res } = await this.$axios.get('menus')
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #ffefe4;
  background-image: linear-gradient(190deg, #ffefe4 1%, #ffcad4 25%);
  height: 100px;
  z-index: 999;
  box-shadow: 0 0 10px 1px #7c7272;
  color: #ffffee;
  font-weight: bold;
  font-size: 40px;
  justify-content: space-between;
  display: flex;
  align-items: center;

  > .header-left {
    height: 100%;
    display: inline-block;
    float: left;

    > span {
      position: relative;
      left: 175px;
      height: 100%;
      line-height: 100px;
      text-shadow: 0 0 5px #575757;
    }
  }

  > .header-right {
    height: 100%;
    display: flex;
    align-items: center;
    float: right;

    .el-button {
      background-color: #fa897b;
      height: 40%;
      border: 2px solid #bbbbbb;
      border-radius: 10px;
      font-weight: bold;
      color: white;
    }
  }

  .avatar_box {
    height: 100px;
    width: 100px;
    border: 10px solid #ffedde;
    border-radius: 50%;
    box-shadow: 0 0 10px #9b8d82;
    left: 0%;
    position: absolute;
    transform: translate(26%, 6%);

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;

    }
  }
}

.el-aside {
  background-color: #add0e4;
  background-image: linear-gradient(0deg, #add0e4 0%, #a7d1df 100%);
  z-index: 888;
  box-shadow: 0 0 10px 1px #51656c;
  padding-top: 40px;
  letter-spacing: 1px;
  overflow-x: hidden;

  .toggle-button {
    background-color: #9dc3d2;

    height: 30px;
    line-height: 35px;
    cursor: pointer;
  }

  .el-menu--collapse .el-submenu.is-active i {
    color: #ffdd94;
  }

  .el-menu {
    width: 100%;
    border-right: 0;

    .index-1 {
      margin-right: 60px;
    }
  }
}

.el-main {
  background-color: #ffffee;
}
</style>
